home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / tools / c2latex.lha / c++2latex / c++2latex.l < prev    next >
Text File  |  1993-08-08  |  8KB  |  332 lines

  1. *
  2.  *  This is a flex input file but should be edited in -*-C-*- mode
  3.  *
  4.  *  C++2LaTeX: Produce prettyprinted LaTeX files from  C++ or C sources.
  5.  *  Copyright (C) 1990 Norbert Kiesel
  6.  *
  7.  *  This program is free software; you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation; either version 1, or (at your option)
  10.  *  any later version.
  11.  *
  12.  *  This program is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with this program; if not, write to the Free Software
  19.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  *
  21.  *  Norbert Kiesel     
  22.  *  RWTH Aachen / Institut f. Informatik III
  23.  *  Ahornstr. 55
  24.  *  D-5100 Aachen
  25.  *  West Germany  
  26.  *
  27.  *  Phone:  +49 241 80-7266
  28.  *  EUNET:  norbert@rwthi3.uucp
  29.  *  USENET: ...!mcvax!unido!rwthi3!norbert
  30.  *  X.400:  norbert@rwthi3.informatik.rwth-aachen.de
  31.  *
  32.  *  Please contact me for any bugs you find in this code or any
  33.  *  improvements! I'd also be very happy to get feedback where and
  34.  *  how frequently this program is used (just drop a little mail :-).
  35.  *
  36.  *  ---------------------------------------------------------------------
  37.  *
  38.  *  C++2LaTeX 2.0:
  39.  *  Produce even more prettyprinted LaTeX files from C++ or C sources.
  40.  *
  41.  *  Copyright (C) 1991    Joerg Heitkoetter
  42.  *  Systems Analysis Group, University of Dortmund, Germany.
  43.  *  (heitkoet@gorbi.informatik.uni-dortmund.de).
  44.  *
  45.  */
  46.  
  47. x STRING BCOMMENT INCLUDE SLASHCOMMENT
  48. s CPLUSPLUS
  49.  
  50. {
  51. define KEY        printf ("{\\%s %s}", keyword_font, yytext)
  52. define CPP        printf ("{\\%s \\%s}", cpp_font, yytext)
  53. define SYM(x)        printf ("$\\%s$", x)
  54. define OUT(x)        printf ("%s", x)
  55. define BTAB        printf ("\\hspace*{%d\\indentation}", tabtobrace)
  56. define CTAB        printf ("\\hspace*{%d\\indentation}", tabtocomment)
  57. define FONT(x)        printf ("{\\%s ",x);
  58. define SUB(x)        substitute(x)
  59. define IND        indent(yytext)
  60. define INIT        BEGIN (cplusplus_mode ? CPLUSPLUS : INITIAL);
  61. define REPARSE        yyless (0)
  62.  
  63. include <stdio.h>
  64.  
  65. ifdef ANSI_C
  66.   #ifdef C_PLUSPLUS
  67.     #error ANSI_C and C_PLUSPLUS are mutually exclusive
  68.   #else
  69.     int cplusplus_mode = 0;
  70.   #endif
  71. else /* CPLUSPLUS or default */
  72.   int cplusplus_mode = 1;
  73. endif
  74.  
  75. nt complete_file    = 0;
  76. nt header        = 0;
  77. nt tabtotab        = 8;
  78. nt piped        = 0;
  79.  
  80. nt aligntoright    = 1;        /* align comments to the right  -joke */
  81. nt tabtobrace        = 2;        /* distance between closing brace and corresponding comment  -joke */
  82. nt tabtocomment    = 4;        /* distance between statement and corresponding comment  -joke */
  83.  
  84. har *font_size        = "10";        /* used to be 11  -joke */
  85. har *indentation    = "0.5em";
  86. har *comment_font    = "it";
  87. har *keyword_font    = "bf";
  88. har *header_font    = "sl";
  89. har *cpp_font        = "tt";
  90. har *string_font    = "tt";
  91.  
  92. ifdef __STDC__
  93. oid substitute(const char *);
  94. oid indent(const char *);
  95. oid newpage(int);
  96. oid usage(const char *);
  97. else
  98. oid substitute();
  99. oid indent();
  100. oid newpage();
  101. oid usage();
  102. endif
  103. }
  104.  
  105. %
  106.  
  107.             INIT;
  108.  
  109. #"[ \t]*"include"    { CPP; BEGIN (INCLUDE); }
  110. #"[ \t]*"define"    |
  111. #"[ \t]*"undef"    |
  112. #"[ \t]*"pragma"    |
  113. #"[ \t]*"if"        |
  114. #"[ \t]*"ifdef"    |
  115. #"[ \t]*"ifndef"       |
  116. #"[ \t]*"elif"        |
  117. #"[ \t]*"else"        |
  118. #"[ \t]*"error"    |
  119. #"[ \t]*"line"         |        /* added G++ special #line directive  -joke */
  120. #"[ \t]*"ex"           |        /* added G++ special #ex directive  -joke */
  121. #"[ \t]*"endif"    CPP;
  122.  
  123.  
  124. INCLUDE>"<"[^>]*/">"    { OUT ("$<$"); FONT (string_font);
  125.               SUB (yytext+1); OUT ("}$>$");
  126.               input(); INIT; }
  127. INCLUDE>\"[^\"]*/\"    { OUT ("\""); FONT (string_font);
  128.               SUB (yytext+1); OUT ("}\"");
  129.               input(); INIT; }
  130. INCLUDE>[ \t]+        ECHO;
  131. INCLUDE>[\n]        OUT ("\\mbox{}\\\\\n");
  132. INCLUDE>.        { REPARSE; INIT; }
  133.  
  134. auto"            |
  135. double"        |
  136. int"            |
  137. struct"        |
  138. break"            |
  139. else"            |
  140. long"            |
  141. switch"        |
  142. case"            |
  143. enum"            |
  144. register"        |
  145. typedef"        |
  146. char"            |
  147. extern"        |
  148. return"        |
  149. union"            |
  150. const"            |
  151. float"            |
  152. short"            |
  153. unsigned"        |
  154. continue"        |
  155. for"            |
  156. signed"        |
  157. void"            |
  158. default"        |
  159. goto"            |
  160. sizeof"        |
  161. volatile"        |
  162. do"            |
  163. if"            |
  164. static"        |
  165. while"            |
  166. CPLUSPLUS>"new"    |
  167. CPLUSPLUS>"delete"    |
  168. CPLUSPLUS>"this"    |
  169. CPLUSPLUS>"operator"    |
  170. CPLUSPLUS>"class"    |
  171. CPLUSPLUS>"public"    |
  172. CPLUSPLUS>"protected"    |
  173. CPLUSPLUS>"private"    |
  174. CPLUSPLUS>"virtual"    |
  175. CPLUSPLUS>"friend"    |
  176. CPLUSPLUS>"inline"    |
  177. CPLUSPLUS>"dynamic"    |        /* added  -joke */
  178. CPLUSPLUS>"typeof"    |        /* added  -joke */
  179. CPLUSPLUS>"all"    |        /* G++ special added  -joke */
  180. CPLUSPLUS>"except"    |        /* G++ special added  -joke */
  181. CPLUSPLUS>"exception"    |        /* G++ special added  -joke */
  182. CPLUSPLUS>"raise"    |        /* G++ special added  -joke */
  183. CPLUSPLUS>"raises"    |        /* G++ special added  -joke */
  184. CPLUSPLUS>"reraise"    |        /* G++ special added  -joke */
  185. CPLUSPLUS>"try"    |        /* G++ special added  -joke */
  186. CPLUSPLUS>"asm"    |        /* added  -joke */
  187. CPLUSPLUS>"catch"    |        /* added  -joke */
  188. CPLUSPLUS>"overload"    KEY;
  189. ->"            SYM ("rightarrow");
  190. <<"            SYM ("ll");
  191. >>"            SYM ("gg");
  192. <="            SYM ("leq");
  193. >="            SYM ("geq");
  194. !="            SYM ("neq");
  195. ||"            SYM ("mid\\mid");
  196. ..."            SYM ("ldots");
  197. *="            SYM ("ast=");
  198. <<="            SYM ("ll=");
  199. >>="            SYM ("gg=");
  200. ^="            SYM ("vee=");
  201. |="            SYM ("mid=");
  202. ~"            SYM ("sim");
  203. *"            SYM ("ast");
  204. ^"            SYM ("wedge");
  205. |"            SYM ("mid");
  206. CPLUSPLUS>"->*"    SYM ("rightarrow\\ast");
  207. /"            OUT ("$/$");
  208. <"            OUT ("$<$");
  209. >"            OUT ("$>$");
  210. &&"            OUT ("\\&\\&");
  211. %="            OUT ("\\%=");
  212. &="            OUT ("\\&=");
  213. {"            OUT ("\\{");
  214. }"            OUT ("\\}");
  215. &"            OUT ("\\&");
  216. %"            OUT ("\\%");
  217. --"            OUT ("-{}-");
  218. CPLUSPLUS>".*"        OUT (".$\\ast$");
  219. ?"            |
  220. :"            |
  221. ="            |
  222. ,"            |
  223. ."            |
  224. ;"            |
  225. !"            |
  226. -"            |
  227. +"            |
  228. /="            |
  229. =="            |
  230. ++"            |
  231. +="            |
  232. -="            |
  233. ("            |
  234. )"            |
  235. ["            |
  236. ]"            |
  237. CPLUSPLUS>"::"        ECHO;
  238.  
  239. CPLUSPLUS>[a-zA-Z_$][a-zA-Z_$0-9]*    |
  240. a-zA-Z_][a-zA-Z_0-9]*            SUB (yytext);
  241.  
  242.  /*    1.   Asterisk comments mini scanner */
  243.  /*    1.1. Curly  brace comment */
  244. }"[ \t]*"/*"                  { BEGIN (BCOMMENT);
  245.                                 OUT ("\\}");
  246.                                 BTAB;
  247.                                 FONT (comment_font);
  248.                                 OUT ("/$\\ast$"); }
  249.  
  250.  /*    1.2. Comments at the beginning of a line */
  251. [ \t]*"/*"              { BEGIN (BCOMMENT);
  252.                 FONT (comment_font);
  253.                                 REPARSE; }
  254.  
  255.  /*    1.3. Other comments, aligned to right side of paper */
  256. /*"                  { BEGIN (BCOMMENT);
  257.                                 if (aligntoright) {
  258.                     OUT ("\\hfill");
  259.                 } else {
  260.                     CTAB;
  261.                 }
  262.                 FONT (comment_font);
  263.                                 OUT ("/$\\ast$"); }
  264.  
  265. BCOMMENT>"*/"              { INIT; OUT ("$\\ast$/}"); }
  266. BCOMMENT>"\n"            OUT ("\\mbox{}\\\\\n");
  267. BCOMMENT>[ \t]+        IND;
  268. BCOMMENT>.            SUB (yytext);
  269.  
  270.  /*    2.   Double slash ``//'' comments mini scanner */
  271.  /*    2.1. Curly brace comment */
  272. CPLUSPLUS>}[ \t]*"//"        { BEGIN (SLASHCOMMENT);
  273.                                 OUT ("\\}");
  274.                                 BTAB;
  275.                 FONT (comment_font);
  276.                                 OUT ("//");}
  277.  
  278.  /*    2.2. Complete line comment */
  279. CPLUSPLUS>^[ \t]*"//"        { BEGIN (SLASHCOMMENT);
  280.                                 FONT (comment_font);
  281.                 REPARSE; }
  282.  
  283.  /*    2.3. Other comments */
  284. CPLUSPLUS>"//"           { BEGIN (SLASHCOMMENT);
  285.                                 if (aligntoright) {
  286.                     OUT ("\\hfill");
  287.                 } else {
  288.                     CTAB;
  289.                 }
  290.                 FONT (comment_font);
  291.                                 OUT ("//"); }
  292.  
  293. SLASHCOMMENT>"\n"          { INIT; OUT ("}\\mbox{}\\\\\n"); }
  294. SLASHCOMMENT>[ \t]+        IND;
  295. SLASHCOMMENT>.                SUB (yytext);
  296.  
  297.  
  298. ?\"                       { BEGIN (STRING);
  299.                 FONT (string_font); OUT ("\""); }
  300. STRING>\\\\            OUT ("$\\backslash\\backslash$");
  301. STRING>\\[bfnrtv'"]          { OUT ("$\\backslash$"); SUB (yytext+1); }
  302. STRING>\"              { INIT; OUT ("\"}"); }
  303. STRING>"\n"            OUT ("\\mbox{}\\\\\n");
  304. STRING>^[ \t]+            IND;
  305. STRING>.            SUB (yytext);
  306.  
  307. [0-9]*\.[0-9]+[fFlL]?)         |
  308. [0-9]+\.[0-9]*[fFlL]?)         |
  309. [0-9]*\.?[0-9]+[eE][+-]?[0-9]+) |
  310. [0-9]+\.?[0-9]*[eE][+-]?[0-9]+) ECHO;
  311.  
  312. 0-9]+[uUlL]?            ECHO;
  313.  
  314. ?'[ -~]'            |
  315. ?'\\[ntvbrfa\\?'"]'        |
  316. ?'\\[0-7]{1,3}'        |
  317. ?'\\x[0-9a-fA-F]{1,2}'        SUB (yytext);
  318.  
  319. [0-7]+[uUlL]?            ECHO;
  320.  
  321. x[0-9a-fA-F]+[uUlL]?        ECHO;
  322.  
  323. \\\n"                OUT ("$\\backslash$\\\\\n");
  324. [ \t]+                IND;
  325.  \t]+                ECHO;
  326. \f"[\n]?            OUT ("\\newpage\n");
  327. \n"                OUT ("\\mbox{}\\\\\n");
  328.  
  329. %
  330.  
  331. include "main.c"
  332.